package com.hx.voice.recognition.auth.client.jwt;

import com.hx.voice.recognition.auth.client.config.UserAuthConfig;
import com.hx.voice.recognition.auth.common.util.jwt.IJWTInfo;
import com.hx.voice.recognition.auth.common.util.jwt.JWTHelper;
import com.hx.voice.recognition.common.exception.auth.ClientTokenException;
import io.jsonwebtoken.ExpiredJwtException;
import io.jsonwebtoken.SignatureException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;

/**
 * @descripe：用户授权工具类
 * @company：南京华讯方舟通讯设备有限公司
 * @author：zhoujinbing
 * @time：2019/6/11 10:34
 * @version：V1.0
 */
@Configuration
public class UserAuthUtil {

    @Autowired
    private UserAuthConfig userAuthConfig;

    /**
     * @description 根据token获取jwt鉴权信息
     * @method  getInfoFormToken
     * @param  [token]
     * @return IJWTInfo
     * @date: 2019/6/11 11:15
     * @author:zhoujinbing
     */
    public IJWTInfo getInfoFormToken(String token) throws Exception
    {
        try {
            return JWTHelper.getInfoFormToken(token,userAuthConfig.getPubKeyByte());
        }catch (ExpiredJwtException ex)
        {
            throw new ClientTokenException("Client token expired!");
        }catch (SignatureException ex)
        {
            throw new ClientTokenException("Client token signature error!");
        }catch (IllegalArgumentException ex)
        {
            throw  new ClientTokenException("Client token is null or empty!");
        }
    }



}